<?php

/**
 * ecshop ERP v1.0
 * ----------------------------------------------------------------------------
 * http://www.phpally.com
 * Jacklee的博客 致力于php技术
 * ----------------------------------------------------------------------------
 * 作者: Jacklee
 * 邮箱: jack349392900@gmail.com
 * 创建时间: 2013-11-18
 * 最后修改时间: 2013-11-18
 */

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require(dirname(__FILE__) . '/includes/lib/lib_erp_base.php');
require(dirname(__FILE__) . '/includes/lib/lib_erp_common.php');
require(dirname(__FILE__) . '/includes/lib/lib_erp_order.php');
require(dirname(__FILE__) . '/includes/lib/lib_erp_supplier.php');
require(dirname(__FILE__) . '/includes/lib/lib_erp_goods.php');
require(dirname(__FILE__) . '/includes/lib/lib_erp_goods_attr.php');


require(dirname(__FILE__) . '/includes/lib/cls/cls_date.php');

if($_REQUEST['act'] == 'order_list')
{	
	if(admin_priv('erp_order_view', '', false) || admin_priv('erp_order_manage', '', false) || admin_priv('erp_order_rate', '', false) || admin_priv('erp_order_approve', '', false) )
	{
		include('./includes/lib/page.class.php');
		
		$smarty->assign('order_status',  get_order_status());
		
		//分页类参数
		$num_per_page=10;
		$mode=1;
		$page_bar_num=6;
		$page_style="page_style";//页码css类
		$current_page_style="current_page_style";//当前页css类
		
		if(!empty($_REQUEST['order_sn']))
		{
			$order_info=get_order_info('',$_REQUEST['order_sn']);
			$total_num=count($order_info);
		}
		else{
			$page=isset($_REQUEST['page'])?($_REQUEST['page']):1;
			$start=$num_per_page*($page-1);
			
			$order_status=isset($_REQUEST['o_s'])?intval($_REQUEST['o_s']):0;
			
			$admin_id=erp_get_admin_id();
			
			if((admin_priv('erp_order_manage', '', false)) && !(admin_priv('erp_order_rate', '', false)) && !(admin_priv('erp_order_approve', '', false)))
			{
				$admin_id=erp_get_admin_id();
				$smarty->assign('supplier_info',  get_admin_supplier(erp_get_admin_id()));
			}				
			else{
				$admin_id=0;
				$smarty->assign('supplier_info',  get_supplier_info());
			}
			
			
			/*
			if((admin_priv('erp_order_manage', '', false)) && !(admin_priv('erp_order_rate', '', false)) && !(admin_priv('erp_order_approve', '', false)))
			{
				$order_status=1;
			}	
			elseif(!(admin_priv('erp_order_manage', '', false)) && (admin_priv('erp_order_rate', '', false)) && !(admin_priv('erp_order_approve', '', false)))
			{
				$order_status=2;
			}					
			elseif(!(admin_priv('erp_order_manage', '', false)) && !(admin_priv('erp_order_rate', '', false)) && (admin_priv('erp_order_approve', '', false)))
			{
				$order_status=3;
			}	
			else{
				$order_status=0;
			}
			*/
			
			$supplier_id=isset($_REQUEST['s_id'])?intval($_REQUEST['s_id']):0;
	
			$cls_date=new cls_date();
			
			$start_time=!empty($_REQUEST['s_date'])? $cls_date->date_to_stamp($_REQUEST['s_date']):'';
			$end_time=!empty($_REQUEST['e_date'])? $cls_date->date_to_stamp($_REQUEST['e_date']):'';

			$total_num=count(get_order_info('','',$order_status,$start_time,$end_time,$supplier_id,$admin_id));
			
			$order_info=get_order_info('','',$order_status,$start_time,$end_time,$supplier_id,$admin_id,$start,$num_per_page);
			
			$url="erp_order_manage.php?act=order_list&o_s=".$order_status."&f_id=".$supplier_id."&s_date=".$start_date."&e_date=".$end_date;
		}

		$page=new page(array('total_data'=>$total_num,'data_per_page'=>$num_per_page,'url'=>$url,'mode'=>$mode,'page_bar_num'=>$page_bar_num,'page_style'=>$page_style,'current_page_style'=>$current_page_style));
		$smarty->assign('page',  $page->show());		
	

		if((admin_priv('erp_order_manage', '', false)))
		{
			$action_link = array('href' => 'erp_order_manage.php?act=add_order', 'text' => $_LANG['erp_add_order']);
			$smarty->assign('action_link',  $action_link);
		}

		$smarty->assign('order_info',  $order_info);
		
		$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_order_list']);
		$smarty->display('erp_order_list.htm');
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}

elseif($_REQUEST['act'] == 'add_order')
{
		
	if((admin_priv('erp_order_manage', '', false)))
	{		
		
		$supplier_info=get_admin_supplier(erp_get_admin_id());
		
		if(empty($supplier_info))
		{
			$href="erp_order_manage.php?act=order_list";
			$text=$_LANG['erp_order_return_to_order_list'];
			$link[] = array('href' => $href, 'text' => $text);	
	
	   	sys_msg($_LANG['erp_order_no_admin_supplier'], 0, $link);			
		}
		
		$order_id=add_order();
		if($order_id!=false)
		{	
				if(lock_order($order_id,'edit'))//锁定订单
				{
					$smarty->assign('url',  $_SERVER["REQUEST_URI"]);
					
	
					$smarty->assign('supplier_info',  $supplier_info);
		
					$order_info=get_order_info($order_id);//获取订单信息
					$order_item_info=get_order_item_info($order_id);//获取订单项信息
				
					$smarty->assign('order_info',  $order_info[0]);
					$smarty->assign('order_item_info',  $order_item_info);	
				
					if($order_info['order_status']==4)
					{
						$href="erp_order_manage.php?act=print_order&order_id=".$order_id."&page=1";
						$text=$_LANG['erp_order_operation_print'];
				
						$action_link = array('href' => $href, 'text' => $text);
						$smarty->assign('action_link',  $action_link);		
					}			
					
					$smarty->assign('act',  'edit');
					
					$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_edit_order']);
					$smarty->display('erp_order_info.htm');	
				}
				else{
					$href="./erp_order_manage.php?act=order_list";
					$text=$_LANG['erp_order_return_to_order_list'];
					$link[] = array('href' => $href, 'text' => $text);	
					
					$href="erp_order_manage.php?act=view_order&order_id=".$order_id;
					$text=$_LANG['erp_order_view_order'];
					$link[] = array('href' => $href, 'text' => $text);
		
		   		sys_msg($_LANG['erp_order_no_accessibility'], 0, $link);
				}				

		}
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}
/* 查看生产通知单 */
elseif($_REQUEST['act'] == 'view_order')
{
	if(admin_priv('erp_order_view', '', false) || admin_priv('erp_order_manage', '', false) || admin_priv('erp_order_rate', '', false) || admin_priv('erp_order_approve', '', false) )
	{		
		$order_id=$_REQUEST['order_id'];

		$order_info=get_order_info($order_id);//获取订单信息
		
		$order_item_info=get_order_item_info($order_id);//获取订单项信息
		if((admin_priv('erp_order_manage', '', false)))
		{
			$action_link = array('href' => 'erp_order_manage.php?act=order_list', 'text' => $_LANG['02_erp_order_list']);
			$smarty->assign('action_link',  $action_link);
		}
		
		$smarty->assign('order_info',  $order_info[0]);
		$smarty->assign('order_item_info',  $order_item_info);	

		$smarty->assign('act',  'view');
		$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_view_order']);
		$smarty->display('erp_order_info.htm');	
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}
}

elseif($_REQUEST['act'] == 'edit_order')
{
	if((admin_priv('erp_order_manage', '', false)))
	{
		$order_id=$_REQUEST['order_id'];
		if(lock_order($order_id,'edit'))//锁定订单
		{
			$smarty->assign('url',  $_SERVER["REQUEST_URI"]);
			$supplier_info=get_admin_supplier(erp_get_admin_id());
		
			$smarty->assign('supplier_info',  $supplier_info);

			$order_info=get_order_info($order_id);//获取订单信息
			$order_item_info=get_order_item_info($order_id);//获取订单项信息
		
			$smarty->assign('order_info',  $order_info[0]);
			$smarty->assign('order_item_info',  $order_item_info);	
		
			if($order_info['order_status']==4)
			{
				$href="erp_order_manage.php?act=print_order&order_id=".$order_id."&page=1";
				$text=$_LANG['erp_order_operation_print'];
		
				$action_link = array('href' => $href, 'text' => $text);
				$smarty->assign('action_link',  $action_link);		
			}			
			if((admin_priv('erp_order_manage', '', false)))
			{
				$action_link = array('href' => 'erp_order_manage.php?act=order_list', 'text' => $_LANG['02_erp_order_list']);
				$smarty->assign('action_link',  $action_link);
			}
		
			$smarty->assign('act',  'edit');
			$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_edit_order']);
			$smarty->display('erp_order_info.htm');	
		}
		else{
			$href="erp_order_manage.php?act=order_list";
			$text=$_LANG['erp_order_return_to_order_list'];
			$link[] = array('href' => $href, 'text' => $text);	
			
			$href="erp_order_manage.php?act=view_order&order_id=".$order_id;
			$text=$_LANG['erp_order_view_order'];
			$link[] = array('href' => $href, 'text' => $text);

   		sys_msg($_LANG['erp_order_no_accessibility'], 0, $link);
		}		
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}

/* 财务定价 */
elseif($_REQUEST['act'] == 'rate_order')
{
	if((admin_priv('erp_order_rate', '', false)))
	{		
		$order_id=$_REQUEST['order_id'];

		if(lock_order($order_id,'rate'))//锁定订单
		{
			$order_info=get_order_info($order_id);//获取订单信息
			$order_item_info=get_order_item_info($order_id);//获取订单项信息
			if((admin_priv('erp_order_manage', '', false)))
			{
				$action_link = array('href' => 'erp_order_manage.php?act=order_list', 'text' => $_LANG['02_erp_order_list']);
				$smarty->assign('action_link',  $action_link);
			}
		
			$smarty->assign('order_info',  $order_info[0]);
			$smarty->assign('order_item_info',  $order_item_info);	
			$smarty->assign('act',  'rate');
			$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_rate_order']);
			$smarty->display('erp_order_info.htm');
		}
		else{
			$href="erp_order_manage.php?act=order_list";
			$text=$_LANG['erp_order_return_to_order_list'];
			$link[] = array('href' => $href, 'text' => $text);	
			
			$href="erp_order_manage.php?act=view_order&order_id=".$order_id;
			$text=$_LANG['erp_order_view_order'];
			$link[] = array('href' => $href, 'text' => $text);

   		sys_msg($_LANG['erp_order_no_accessibility'], 0, $link);
		}
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}


/* 审核订单*/
elseif($_REQUEST['act'] == 'approve_order')
{
	if((admin_priv('erp_order_approve', '', false)))
	{	
		$order_id=$_REQUEST['order_id'];

		$order_info=get_order_info($order_id);//获取订单信息
		$order_item_info=get_order_item_info($order_id);//获取订单项信息
		if((admin_priv('erp_order_manage', '', false)))
		{
			$action_link = array('href' => 'erp_order_manage.php?act=order_list', 'text' => $_LANG['02_erp_order_list']);
			$smarty->assign('action_link',  $action_link);
		}
		
		$smarty->assign('order_info',  $order_info[0]);
		$smarty->assign('order_item_info',  $order_item_info);	
		$smarty->assign('act',  'approve');
		$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_approve_order']);
		$smarty->display('erp_order_info.htm');	
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}
}

/* 执行订单*/
elseif($_REQUEST['act'] == 'execute_order')
{
	if((admin_priv('erp_order_execute', '', false)))
	{	
		$order_id=$_REQUEST['order_id'];

		$order_info=get_order_info($order_id);//获取订单信息
		$order_item_info=get_order_item_info($order_id);//获取订单项信息
		if((admin_priv('erp_order_manage', '', false)))
		{
			$action_link = array('href' => 'erp_order_manage.php?act=order_list', 'text' => $_LANG['02_erp_order_list']);
			$smarty->assign('action_link',  $action_link);
		}
		
		$smarty->assign('order_info',  $order_info[0]);
		$smarty->assign('order_item_info',  $order_item_info);	
		$smarty->assign('act',  'execute');
		$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_execute_order']);
		$smarty->display('erp_order_info.htm');	
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}
}

/* 完结订单*/
elseif($_REQUEST['act'] == 'finish_order')
{
	if((admin_priv('erp_order_finish', '', false)))
	{	
		$order_id=$_REQUEST['order_id'];

		$order_info=get_order_info($order_id);//获取订单信息
		$order_item_info=get_order_item_info($order_id);//获取订单项信息
		if((admin_priv('erp_order_manage', '', false)))
		{
			$action_link = array('href' => 'erp_order_manage.php?act=order_list', 'text' => $_LANG['02_erp_order_list']);
			$smarty->assign('action_link',  $action_link);
		}
		$smarty->assign('order_info',  $order_info[0]);
		$smarty->assign('order_item_info',  $order_item_info);	
		$smarty->assign('act',  'finish');
		$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_finish_order']);
		$smarty->display('erp_order_info.htm');	
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}
}
/* 打印订单*/
elseif($_REQUEST['act'] == 'print_order')
{
	if(admin_priv('erp_order_manage', '', false) || admin_priv('erp_order_rate', '', false) || admin_priv('erp_order_approve', '', false))
	{	
		$order_id=$_REQUEST['order_id'];

		$order_info=get_order_info($order_id);//获取订单信息
		$order_item_info=get_order_item_info($order_id);//获取订单项信息
		
		$smarty->assign('order_info',  $order_info[0]);
		$smarty->assign('order_item_info',  $order_item_info);	
		
		$smarty->assign('shop_name',  $_CFG['shop_name']);
		
		//echo "<pre>";
			//print_r($order_info[0]);
		//echo "</pre>";
		//echo "<pre>";
			//print_r($order_item_info);
		//echo "</pre>";		
		
		$smarty->assign('total_page',  count($order_item_info));
			
		$smarty->display('erp_order_print.htm');	
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}
}
?>